﻿#include <iostream>
#include <fstream>
#include "swi_typical_parameter.h"

namespace Swi
{
    TypicalParameter::TypicalParameter()
    {
    }

    TypicalParameter::~TypicalParameter()
    {
    }

    std::vector<Swi::TypicalGenerator> ReadTypicalSwi(std::string filename)
    {
        std::vector<TypicalGenerator> typical_generator_list;
        typical_generator_list.clear();

        std::ifstream in_file(filename.c_str(), std::ios::in);
        if (in_file.fail())
        {
            // TODO: 输出日志
        }

        struct StartEndLineNum
        {
            int start_line_num;
            int end_line_num;
        };
        // 火电、热电、水电、风电类型开始、结束位置
        StartEndLineNum types_start_end[4] = { 0 };

        /* 类型 */
        int line_num = 0;
        while (!in_file.eof())
        {
            line_num++;
            std::string line_str;
            std::getline(in_file, line_str);

            if (strstr(line_str.c_str(), ".<类型=火电>"))
            {
                types_start_end[0].start_line_num = line_num;
            }
            else if (strstr(line_str.c_str(), ".</类型=火电>"))
            {
                types_start_end[0].end_line_num = line_num;
            }

            else if (strstr(line_str.c_str(), ".<类型=热电>"))
            {
                types_start_end[1].start_line_num = line_num;
            }
            else if (strstr(line_str.c_str(), ".</类型=热电>"))
            {
                types_start_end[1].end_line_num = line_num;
            }

            else if (strstr(line_str.c_str(), ".<类型=水电>"))
            {
                types_start_end[2].start_line_num = line_num;
            }
            else if (strstr(line_str.c_str(), ".</类型=水电>"))
            {
                types_start_end[2].end_line_num = line_num;
            }

            else if (strstr(line_str.c_str(), ".<类型=风电>"))
            {
                types_start_end[3].start_line_num = line_num;
            }
            else if (strstr(line_str.c_str(), ".</类型=风电>"))
            {
                types_start_end[3].end_line_num = line_num;
            }
        }

        /* 机组 */
        in_file.clear();
        in_file.seekg(0, std::ios_base::beg);
        // 机组的数目
        int generator_num = 0;
        line_num = 0;
        while (!in_file.eof())
        {
            line_num++;
            std::string line_str;
            std::getline(in_file, line_str);

            if (line_num > types_start_end[0].start_line_num && line_num < types_start_end[0].end_line_num)
            {
                TypicalGenerator typical_generator;
                if (strstr(line_str.c_str(), ".<机组="))
                {
                    generator_num++;
                    typical_generator.line_num_start = line_num;
                    typical_generator.name = line_str.substr((line_str.find_first_of('=')), (line_str.find_last_of('>')));
                    typical_generator_list.push_back(typical_generator);
                }
                else if (strstr(line_str.c_str(), ".</机组="))
                {
                    typical_generator_list[generator_num - 1].line_num_end = line_num;
                }
            }

            else if (line_num > types_start_end[1].start_line_num && line_num < types_start_end[1].end_line_num)
            {
                TypicalGenerator typical_generator;
                if (strstr(line_str.c_str(), ".<机组="))
                {
                    generator_num++;
                    typical_generator.line_num_start = line_num;
                    typical_generator.name = line_str.substr((line_str.find_first_of('=')), (line_str.find_last_of('>')));
                    typical_generator_list.push_back(typical_generator);
                }
                else if (strstr(line_str.c_str(), ".</机组="))
                {
                    typical_generator_list[generator_num - 1].line_num_end = line_num;
                }
            }

            else if (line_num > types_start_end[2].start_line_num && line_num < types_start_end[2].end_line_num)
            {
                TypicalGenerator typical_generator;
                if (strstr(line_str.c_str(), ".<机组="))
                {
                    generator_num++;
                    typical_generator.line_num_start = line_num;
                    typical_generator.name = line_str.substr((line_str.find_first_of('=')), (line_str.find_last_of('>')));
                    typical_generator_list.push_back(typical_generator);
                }
                else if (strstr(line_str.c_str(), ".</机组="))
                {
                    typical_generator_list[generator_num - 1].line_num_end = line_num;
                }
            }

            else if (line_num > types_start_end[3].start_line_num && line_num < types_start_end[3].end_line_num)
            {
                TypicalGenerator typical_generator;
                if (strstr(line_str.c_str(), ".<机组="))
                {
                    generator_num++;
                    typical_generator.line_num_start = line_num;
                    typical_generator.name = line_str.substr((line_str.find_first_of('=')), (line_str.find_last_of('>')));
                    typical_generator_list.push_back(typical_generator);
                }
                else if (strstr(line_str.c_str(), ".</机组="))
                {
                    typical_generator_list[generator_num - 1].line_num_end = line_num;
                }
            }

        }

        /* 卡片 */
        line_num = 0;
        in_file.clear();
        in_file.seekg(0, std::ios_base::beg);
        while (!in_file.eof())
        {
            line_num++;
            std::string line_str;
            std::getline(in_file, line_str);

            for (std::vector<TypicalGenerator>::iterator se = typical_generator_list.begin(); se != typical_generator_list.end(); se++)
            {
                if (line_num > se->line_num_start && line_num < se->line_num_end)
                {
                    /******** generator ********/

                    //M
                    if (line_str.at(0) == 'M' && line_str.at(1) == ' ')
                    {
                        se->m.card_type = line_str.substr(0, 1);
                        se->m.line_num = line_num;
                        se->m.card_str = line_str;
                        se->voltage = atof(line_str.substr(11, 4).c_str());
                        se->capacity = atof(line_str.substr(16, 5).c_str());
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //MCF
                    else if (line_str.at(0) == 'M' &&\
                        (line_str.at(1) == 'C' || line_str.at(1) == 'F'))
                    {
                        se->mcf.card_type = line_str.substr(0, 2);
                        se->mcf.line_num = line_num;
                        se->mcf.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //MG
                    else if (line_str.at(0) == 'M' && line_str.at(1) == 'G')
                    {
                        se->mg.card_type = line_str.substr(0, 2);
                        se->mg.line_num = line_num;
                        se->mg.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    /******** exitation ********/

                    //E
                    else if (line_str.at(0) == 'E'
                        && (line_str.at(1) == 'A' || line_str.at(1) == 'B'
                        || line_str.at(1) == 'C' || line_str.at(1) == 'D'
                        || line_str.at(1) == 'E' || line_str.at(1) == 'F'
                        || line_str.at(1) == 'G' || line_str.at(1) == 'K'
                        || line_str.at(1) == 'J'))
                    {
                        se->e.card_type = line_str.substr(0, 2);
                        se->e.line_num = line_num;
                        se->e.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //F
                    else if (line_str.at(0) == 'F'
                        && (line_str.at(1) == 'A' || line_str.at(1) == 'B'
                        || line_str.at(1) == 'C' || line_str.at(1) == 'D'
                        || line_str.at(1) == 'E' || line_str.at(1) == 'L'
                        || line_str.at(1) == 'G' || line_str.at(1) == 'H'
                        || line_str.at(1) == 'J' || line_str.at(1) == 'K'))
                    {
                        se->f.card_type = line_str.substr(0, 2);
                        se->f.line_num = line_num;
                        se->f.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //FZ
                    else if (line_str.at(0) == 'F' && line_str.at(1) == 'Z')
                    {
                        se->fz.card_type = line_str.substr(0, 2);
                        se->fz.line_num = line_num;
                        se->fz.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //FMV
                    else if (line_str.at(0) == 'F'
                        && (line_str.at(1) == 'M' || line_str.at(1) == 'N'
                        || line_str.at(1) == 'O' || line_str.at(1) == 'P'
                        || line_str.at(1) == 'Q' || line_str.at(1) == 'R'
                        || line_str.at(1) == 'S' || line_str.at(1) == 'T'
                        || line_str.at(1) == 'U' || line_str.at(1) == 'V'))
                    {
                        se->fmv.card_type = line_str.substr(0, 2);
                        se->fmv.line_num = line_num;
                        se->fmv.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //Fplus
                    else if (line_str.at(0) == 'F' && line_str.at(1) == '+')
                    {
                        se->fplus.card_type = line_str.substr(0, 2);
                        se->fplus.line_num = line_num;
                        se->fplus.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    /******** pss ********/

                    // SS
                    else if (line_str.at(0) == 'S' && line_str.at(1) == 'S')
                    {
                        se->ss.card_type = line_str.substr(0, 2);
                        se->ss.line_num = line_num;
                        se->ss.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    // SF
                    else if (line_str.at(0) == 'S' && line_str.at(1) == 'F')
                    {
                        se->sf.card_type = line_str.substr(0, 2);
                        se->sf.line_num = line_num;
                        se->sf.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    // SP
                    else if (line_str.at(0) == 'S' && line_str.at(1) == 'P')
                    {
                        se->sp.card_type = line_str.substr(0, 2);
                        se->sp.line_num = line_num;
                        se->sp.card_str = line_str;
                    }

                    // SG
                    else if (line_str.at(0) == 'S' && line_str.at(1) == 'G')
                    {
                        se->sg.card_type = line_str.substr(0, 2);
                        se->sg.line_num = line_num;
                        se->sg.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //SI
                    else if (line_str.at(0) == 'S' && line_str.at(1) == 'I'
                        && line_str.at(2) == ' ')
                    {
                        se->si.card_type = line_str.substr(0, 2);
                        se->si.line_num = line_num;
                        se->si.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //SIplus
                    else if (line_str.at(0) == 'S' && line_str.at(1) == 'I' \
                        && line_str.at(2) == '+')
                    {
                        se->siplus.card_type = line_str.substr(0, 3);
                        se->siplus.line_num = line_num;
                        se->siplus.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    /******** governor primer mover ********/

                    //GG
                    else if (line_str.at(0) == 'G' && line_str.at(1) == 'G')
                    {
                        se->gg.card_type = line_str.substr(0, 2);
                        se->gg.line_num = line_num;
                        se->gg.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //GHht
                    else if (line_str.at(0) == 'G' && line_str.at(1) == 'H')
                    {
                        se->gh.card_type = line_str.substr(0, 2);
                        se->gh.line_num = line_num;
                        se->gh.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //GS
                    else if (line_str.at(0) == 'G' && line_str.at(1) == 'S')
                    {
                        se->gs.card_type = line_str.substr(0, 2);
                        se->gs.line_num = line_num;
                        se->gs.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //GA
                    else if (line_str.at(0) == 'G' && line_str.at(1) == 'A')
                    {
                        se->ga.card_type = line_str.substr(0, 2);
                        se->ga.line_num = line_num;
                        se->ga.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //GI
                    else if (line_str.at(0) == 'G' && line_str.at(1) == 'I' && line_str.at(2) == ' ')
                    {
                        se->gi.card_type = line_str.substr(0, 2);
                        se->gi.line_num = line_num;
                        se->gi.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //GIplus
                    else if (line_str.at(0) == 'G' && line_str.at(1) == 'I' && line_str.at(2) == '+')
                    {
                        se->giplus.card_type = line_str.substr(0, 3);
                        se->giplus.line_num = line_num;
                        se->giplus.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //GJ
                    else if (line_str.at(0) == 'G' && line_str.at(1) == 'J' && line_str.at(2) == ' ')
                    {
                        se->gj.card_type = line_str.substr(0, 2);
                        se->gj.line_num = line_num;
                        se->gj.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //TA
                    else if (line_str.at(0) == 'T' && line_str.at(1) == 'A')
                    {
                        se->ta.card_type = line_str.substr(0, 2);
                        se->ta.line_num = line_num;
                        se->ta.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //TB
                    else if (line_str.at(0) == 'T' && line_str.at(1) == 'B')
                    {

                        se->tb.card_type = line_str.substr(0, 2);
                        se->tb.line_num = line_num;
                        se->tb.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    /******** new energy power ********/

                    //GE
                    else if (line_str.at(0) == 'G' && line_str.at(1) == 'E')
                    {
                        if (line_str.at(2) == ' ')
                        {
                            se->ge.card_type = line_str.substr(0, 2);
                            se->ge.line_num = line_num;
                            se->ge.card_str = line_str;
                            se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                        }
                        else if (line_str.at(2) == '+')
                        {
                            se->geplus.card_type = line_str.substr(0, 3);
                            se->geplus.line_num = line_num;
                            se->geplus.card_str = line_str;
                            se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                        }
                    }

                    //MX
                    else if (line_str.at(0) == 'M' && line_str.at(1) == 'X')
                    {
                        se->mx.card_type = line_str.substr(0, 2);
                        se->mx.line_num = line_num;
                        se->mx.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //EZ
                    else if (line_str.at(0) == 'E' && line_str.at(1) == 'Z')
                    {
                        se->ez.card_type = line_str.substr(0, 2);
                        se->ez.line_num = line_num;
                        se->ez.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //MW
                    else if (line_str.at(0) == 'M' && line_str.at(1) == 'W')
                    {
                        se->mw.card_type = line_str.substr(0, 2);
                        se->mw.line_num = line_num;
                        se->mw.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //WINDG_GE
                    else if (line_str.at(0) == 'W' && line_str.at(1) == 'I' && line_str.at(2) == 'N')
                    {
                        se->wind.card_type = line_str.substr(0, 8);
                        se->wind.line_num = line_num;
                        se->wind.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //RE
                    else if (line_str.at(0) == 'R' && line_str.at(1) == 'E' && line_str.at(2) == ' ')
                    {
                        se->re.card_type = line_str.substr(0, 2);
                        se->re.line_num = line_num;
                        se->re.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                    //REplus
                    else if (line_str.at(0) == 'R' && line_str.at(1) == 'E' && line_str.at(2) == '+')
                    {
                        se->replus.card_type = line_str.substr(0, 3);
                        se->replus.line_num = line_num;
                        se->replus.card_str = line_str;
                        se->generator_cards_str = se->generator_cards_str + line_str + '\n';
                    }

                }

            }
        }

        // 发电机组类型
        for (std::vector<TypicalGenerator>::iterator tg = typical_generator_list.begin(); tg != typical_generator_list.end(); tg++)
        {
            if (tg->line_num_end < types_start_end[0].end_line_num)
            {
                tg->type = "火电";
            }
            else if (tg->line_num_end < types_start_end[1].end_line_num)
            {
                tg->type = "热电";
            }
            else if (tg->line_num_end < types_start_end[2].end_line_num)
            {
                tg->type = "水电";
            }
            else if (tg->line_num_end < types_start_end[3].end_line_num)
            {
                tg->type = "风电";
            }
        }

        in_file.close();

        return typical_generator_list;
    }

    int TypicalGeneratorReplace(Swi::SwiFile swi_file, Swi::TypicalGenerator typical_generator, std::string replace_generator_name)
    {
        for (std::vector<Swi::Region>::iterator region = swi_file.regions.begin(); region != swi_file.regions.end(); region++)
        {
            for (std::vector<Swi::Province>::iterator province = region->provinces.begin(); province != region->provinces.end(); province++)
            {
                /* generator */
                Swi::Generator generator = province->generator;

                // M
                for (std::vector<Swi::M>::iterator m = generator.m_list.begin(); m != province->generator.m_list.end(); m++)
                {
                    if (m->name == replace_generator_name)
                    {
                        generator.m_list.erase(m);
                    }
                    generator.m_list.push_back(typical_generator.m);
                }

                // MCF
                for (std::vector<Swi::MCF>::iterator mcf = generator.mcf_list.begin(); mcf != generator.mcf_list.end(); mcf++)
                {
                    if (mcf->name == replace_generator_name)
                    {
                        generator.mcf_list.erase(mcf);
                    }
                    generator.mcf_list.push_back(typical_generator.mcf);
                }

                // MG
                for (std::vector<Swi::MG>::iterator mg = generator.mg_list.begin(); mg != generator.mg_list.end(); mg++)
                {
                    if (mg->name == replace_generator_name)
                    {
                        generator.mg_list.erase(mg);
                    }
                    generator.mg_list.push_back(typical_generator.mg);
                }

                /* excitation */
                Swi::Excitation excitation = province->excitation;

                // E
                for (std::vector<Swi::E>::iterator e = excitation.e_list.begin(); e != excitation.e_list.end(); e++)
                {
                    if (e->name == replace_generator_name)
                    {
                        excitation.e_list.erase(e);
                    }
                    excitation.e_list.push_back(typical_generator.e);
                }

                // F
                for (std::vector<Swi::F>::iterator f = excitation.f_list.begin(); f != excitation.f_list.end(); f++)
                {
                    if (f->name == replace_generator_name)
                    {
                        excitation.f_list.erase(f);
                    }
                    excitation.f_list.push_back(typical_generator.f);
                }

                // FZ
                for (std::vector<Swi::FZ>::iterator fz = excitation.fz_list.begin(); fz != excitation.fz_list.end(); fz++)
                {
                    if (fz->name == replace_generator_name)
                    {
                        excitation.fz_list.erase(fz);
                    }
                    excitation.fz_list.push_back(typical_generator.fz);
                }

                // FMV
                for (std::vector<Swi::FMV>::iterator fmv = excitation.fmv_list.begin(); fmv != excitation.fmv_list.end(); fmv++)
                {
                    if (fmv->name == replace_generator_name)
                    {
                        excitation.fmv_list.erase(fmv);
                    }
                    excitation.fmv_list.push_back(typical_generator.fmv);
                }

                // Fplus
                for (std::vector<Swi::Fplus>::iterator fplus = excitation.fplus_list.begin(); fplus != excitation.fplus_list.end(); fplus++)
                {
                    if (fplus->name == replace_generator_name)
                    {
                        excitation.fplus_list.erase(fplus);
                    }
                    excitation.fplus_list.push_back(typical_generator.fplus);
                }

                /* pss */
                Swi::PSS pss_block = province->pss;

                // SF
                for (std::vector<Swi::SF>::iterator sf = pss_block.sf_list.begin(); sf != pss_block.sf_list.end(); sf++)
                {
                    if (sf->name == replace_generator_name)
                    {
                        pss_block.sf_list.erase(sf);
                    }
                    pss_block.sf_list.push_back(typical_generator.sf);
                }

                // SP
                for (std::vector<Swi::SP>::iterator sp = pss_block.sp_list.begin(); sp != pss_block.sp_list.end(); sp++)
                {
                    if (sp->name == replace_generator_name)
                    {
                        pss_block.sp_list.erase(sp);
                    }
                    pss_block.sp_list.push_back(typical_generator.sp);
                }

                // SG
                for (std::vector<Swi::SG>::iterator sg = pss_block.sg_list.begin(); sg != pss_block.sg_list.end(); sg++)
                {
                    if (sg->name == replace_generator_name)
                    {
                        pss_block.sg_list.erase(sg);
                    }
                    pss_block.sg_list.push_back(typical_generator.sg);
                }

                // SS
                for (std::vector<Swi::SS>::iterator ss = pss_block.ss_list.begin(); ss != pss_block.ss_list.end(); ss++)
                {
                    if (ss->name == replace_generator_name)
                    {
                        pss_block.ss_list.erase(ss);
                    }
                    pss_block.ss_list.push_back(typical_generator.ss);
                }

                // SI
                for (std::vector<Swi::SI>::iterator si = pss_block.si_list.begin(); si != pss_block.si_list.end(); si++)
                {
                    if (si->name == replace_generator_name)
                    {
                        pss_block.si_list.erase(si);
                    }
                    pss_block.si_list.push_back(typical_generator.si);
                }

                // SIplus
                for (std::vector<Swi::SIplus>::iterator siplus = pss_block.siplus_list.begin(); siplus != pss_block.siplus_list.end(); siplus++)
                {
                    if (siplus->name == replace_generator_name)
                    {
                        pss_block.siplus_list.erase(siplus);
                    }
                    pss_block.siplus_list.push_back(typical_generator.siplus);
                }

                /* governor and primer mover */
                Swi::GovernorPrimeMover governor = province->governor_primer_mover;

                // GG
                for (std::vector<Swi::GG>::iterator gg = governor.gg_list.begin(); gg != governor.gg_list.end(); gg++)
                {
                    if (gg->name == replace_generator_name)
                    {
                        governor.gg_list.erase(gg);
                    }
                    governor.gg_list.push_back(typical_generator.gg);
                }

                // GHht
                for (std::vector<Swi::GHht>::iterator ghht = governor.ghht_list.begin(); ghht != governor.ghht_list.end(); ghht++)
                {
                    if (ghht->name == replace_generator_name)
                    {
                        governor.ghht_list.erase(ghht);
                    }
                    governor.ghht_list.push_back(typical_generator.gh);
                }

                // GS
                for (std::vector<Swi::GS>::iterator gs = governor.gs_list.begin(); gs != governor.gs_list.end(); gs++)
                {
                    if (gs->name == replace_generator_name)
                    {
                        governor.gs_list.erase(gs);
                    }
                    governor.gs_list.push_back(typical_generator.gs);
                }

                // GA
                for (std::vector<Swi::GA>::iterator ga = governor.ga_list.begin(); ga != governor.ga_list.end(); ga++)
                {
                    if (ga->name == replace_generator_name)
                    {
                        governor.ga_list.erase(ga);
                    }
                    governor.ga_list.push_back(typical_generator.ga);
                }

                // GI
                for (std::vector<Swi::GI>::iterator gi = governor.gi_list.begin(); gi != governor.gi_list.end(); gi++)
                {
                    if (gi->name == replace_generator_name)
                    {
                        governor.gi_list.erase(gi);
                    }
                    governor.gi_list.push_back(typical_generator.gi);
                }

                // GIplus
                for (std::vector<Swi::GIplus>::iterator giplus = governor.giplus_list.begin(); giplus != governor.giplus_list.end(); giplus++)
                {
                    if (giplus->name == replace_generator_name)
                    {
                        governor.giplus_list.erase(giplus);
                    }
                    governor.giplus_list.push_back(typical_generator.giplus);
                }

                // GJ
                for (std::vector<Swi::GJ>::iterator gj = governor.gj_list.begin(); gj != governor.gj_list.end(); gj++)
                {
                    if (gj->name == replace_generator_name)
                    {
                        governor.gj_list.erase(gj);
                    }
                    governor.gj_list.push_back(typical_generator.gj);
                }

                // TA
                for (std::vector<Swi::TA>::iterator ta = governor.ta_list.begin(); ta != governor.ta_list.end(); ta++)
                {
                    if (ta->name == replace_generator_name)
                    {
                        governor.ta_list.erase(ta);
                    }
                    governor.ta_list.push_back(typical_generator.ta);
                }

                // TB
                for (std::vector<Swi::TB>::iterator tb = governor.tb_list.begin(); tb != governor.tb_list.end(); tb++)
                {
                    if (tb->name == replace_generator_name)
                    {
                        governor.tb_list.erase(tb);
                    }
                    governor.tb_list.push_back(typical_generator.tb);
                }

                /* new energy power */
                Swi::NewEnergyPower new_energy = province->new_energy_power;

                // GE
                for (std::vector<Swi::GE>::iterator ge = new_energy.ge_list.begin(); ge != new_energy.ge_list.end(); ge++)
                {
                    if (ge->name == replace_generator_name)
                    {
                        new_energy.ge_list.erase(ge);
                    }
                    new_energy.ge_list.push_back(typical_generator.ge);
                }

                // GEplus
                for (std::vector<Swi::GEplus>::iterator geplus = new_energy.geplus_list.begin(); geplus != new_energy.geplus_list.end(); geplus++)
                {
                    if (geplus->name == replace_generator_name)
                    {
                        new_energy.geplus_list.erase(geplus);
                    }
                    new_energy.geplus_list.push_back(typical_generator.geplus);
                }

                // MX
                for (std::vector<Swi::MX>::iterator mx = new_energy.mx_list.begin(); mx != new_energy.mx_list.end(); mx++)
                {
                    if (mx->name == replace_generator_name)
                    {
                        new_energy.mx_list.erase(mx);
                    }
                    new_energy.mx_list.push_back(typical_generator.mx);
                }

                // EZ
                for (std::vector<Swi::EZ>::iterator ez = new_energy.ez_list.begin(); ez != new_energy.ez_list.end(); ez++)
                {
                    if (ez->name == replace_generator_name)
                    {
                        new_energy.ez_list.erase(ez);
                    }
                    new_energy.ez_list.push_back(typical_generator.ez);
                }

                // MW
                for (std::vector<Swi::MW>::iterator mw = new_energy.mw_list.begin(); mw != new_energy.mw_list.end(); mw++)
                {
                    if (mw->name == replace_generator_name)
                    {
                        new_energy.mw_list.erase(mw);
                    }
                    new_energy.mw_list.push_back(typical_generator.mw);
                }

                // TG
                for (std::vector<Swi::TG>::iterator tg = new_energy.tg_list.begin(); tg != new_energy.tg_list.end(); tg++)
                {
                    if (tg->name == replace_generator_name)
                    {
                        new_energy.tg_list.erase(tg);
                    }
                    new_energy.tg_list.push_back(typical_generator.tg);
                }

                // GB
                for (std::vector<Swi::GB>::iterator gb = new_energy.gb_list.begin(); gb != new_energy.gb_list.end(); gb++)
                {
                    if (gb->name == replace_generator_name)
                    {
                        new_energy.gb_list.erase(gb);
                    }
                    new_energy.gb_list.push_back(typical_generator.gb);
                }

                // WINDG_GE
                for (std::vector<Swi::WINDG_GE>::iterator windg = new_energy.wind_list.begin(); windg != new_energy.wind_list.end(); windg++)
                {
                    if (windg->name == replace_generator_name)
                    {
                        new_energy.wind_list.erase(windg);
                    }
                    new_energy.wind_list.push_back(typical_generator.wind);
                }

                // RE
                for (std::vector<Swi::RE>::iterator re = new_energy.re_list.begin(); re != new_energy.re_list.end(); re++)
                {
                    if (re->name == replace_generator_name)
                    {
                        new_energy.re_list.erase(re);
                    }
                    new_energy.re_list.push_back(typical_generator.re);
                }

                // REplus
                for (std::vector<Swi::REplus>::iterator replus = new_energy.replus_list.begin(); replus != new_energy.replus_list.end(); replus++)
                {
                    if (replus->name == replace_generator_name)
                    {
                        new_energy.replus_list.erase(replus);
                    }
                    new_energy.replus_list.push_back(typical_generator.replus);
                }
            }

        }

        return 1;
    }

}
